package com.lk.ii.controller;

import java.util.List;

import com.lk.framework.util.ShiroUtils;
import com.lk.ii.domain.IiAuthor;
import com.lk.ii.domain.IiCategory;
import com.lk.ii.service.IIiAuthorService;
import com.lk.ii.service.IIiCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lk.common.annotation.Log;
import com.lk.common.enums.BusinessType;
import com.lk.ii.domain.IiVideo;
import com.lk.ii.service.IIiVideoService;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.common.core.page.TableDataInfo;

/**
 * 视频Controller
 * 
 * @author lk
 * @date 2021-02-05
 */
@Controller
@RequestMapping("/ii/video")
public class IiVideoController extends BaseController
{
    private String prefix = "ii/video";

    @Autowired
    private IIiVideoService iiVideoService;

    @Autowired
    private IIiCategoryService iiCategoryService;

    @Autowired
    private IIiAuthorService iiAuthorService;

    @RequiresPermissions("ii:video:view")
    @GetMapping()
    public String video()
    {
        return prefix + "/video";
    }

    /**
     * 查询视频列表
     */
    @RequiresPermissions("ii:video:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiVideo iiVideo)
    {
        startPage();
        List<IiVideo> list = iiVideoService.selectIiVideoList(iiVideo);
        return getDataTable(list);
    }

    /**
     * 导出视频列表
     */
    @RequiresPermissions("ii:video:export")
    @Log(title = "视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IiVideo iiVideo)
    {
        List<IiVideo> list = iiVideoService.selectIiVideoList(iiVideo);
        ExcelUtil<IiVideo> util = new ExcelUtil<IiVideo>(IiVideo.class);
        return util.exportExcel(list, "video");
    }

    /**
     * 新增视频
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //分类列表
        IiCategory iiCategory = new IiCategory();
        iiCategory.setType("video");
        iiCategory.setStatus(1);
        List<IiCategory> categories=iiCategoryService.selectIiCategoryList(iiCategory);
        mmap.put("categories",categories);
        //作者列表
        IiAuthor iiAuthor = new IiAuthor();
        iiAuthor.setStatus(1);
        List<IiAuthor> authors=iiAuthorService.selectIiAuthorList(iiAuthor);
        mmap.put("authors",authors);
        return prefix + "/add";
    }

    /**
     * 新增保存视频
     */
    @RequiresPermissions("ii:video:add")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiVideo iiVideo)
    {
        iiVideo.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiVideoService.insertIiVideo(iiVideo));
    }

    /**
     * 修改视频
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        //分类列表
        IiCategory iiCategory = new IiCategory();
        iiCategory.setType("video");
        iiCategory.setStatus(1);
        List<IiCategory> categories=iiCategoryService.selectIiCategoryList(iiCategory);
        mmap.put("categories",categories);
        //作者列表
        IiAuthor iiAuthor = new IiAuthor();
        iiAuthor.setStatus(1);
        List<IiAuthor> authors=iiAuthorService.selectIiAuthorList(iiAuthor);
        mmap.put("authors",authors);
        IiVideo iiVideo = iiVideoService.selectIiVideoById(id);
        mmap.put("iiVideo", iiVideo);
        return prefix + "/edit";
    }

    /**
     * 修改保存视频
     */
    @RequiresPermissions("ii:video:edit")
    @Log(title = "视频", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiVideo iiVideo)
    {
        iiVideo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiVideoService.updateIiVideo(iiVideo));
    }

    /**
     * 删除视频
     */
    @RequiresPermissions("ii:video:remove")
    @Log(title = "视频", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iiVideoService.deleteIiVideoByIds(ids));
    }
}
